package com.mall.web.control.app.home;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONArray;
import com.mall.biz.comment.MallCommentBizImpl;
import com.mall.biz.home.HomeBizImpl;
import com.mall.biz.mall.ZhiHuiMallBiz;
import com.mall.biz.search.MallSearchBizImpl;
import com.mall.client.home.MallActivityBookService;
import com.mall.client.home.MallBannerService;
import com.mall.client.mall.MallActivityService;
import com.mall.client.mall.MallService;
import com.mall.client.mallCategory.MallCategoryService;
import com.mall.client.mallFeature.MallFeatureService;
import com.mall.client.mallGroupBuy.MallGroupbBuyService;
import com.mall.client.region.AreaService;
import com.mall.client.region.CityService;
import com.mall.client.region.ProvinceService;
import com.mall.common.util.PageUtil;
import com.mall.enums.BannerEnum;
import com.mall.enums.MallActivityEnum;
import com.mall.enums.MallFeatureEnum;
import com.mall.pojo.Mall;
import com.mall.pojo.MallActivity;
import com.mall.pojo.MallActivityBook;
import com.mall.pojo.MallBanner;
import com.mall.pojo.MallCategory;
import com.mall.pojo.MallFeature;
import com.mall.pojo.MallGroupBuy;
import com.mall.pojo.MallItemOrder;
import com.mall.pojo.User;
import com.mall.query.MallActivityQuery;
import com.mall.query.MallBannerQuery;
import com.mall.query.Page;
import com.mall.query.search.MallSearchQuery;
import com.mall.vo.item.MallItemVO;
import com.mall.vo.mall.MallListVO;
import com.mall.vo.user.AliTonghua;
import com.mall.vo.user.MallCommentVO;
import com.mall.web.annotation.UserLoginMessage;
import com.mall.web.util.APPCookieUtil;
import com.mall.web.util.HttpUtil;
import com.mall.web.util.StringUtil;
import com.mall.web.util.WebUtil;

@Controller
@RequestMapping("/home/list/")
public class MallListController {
	@Autowired
	private MallCategoryService mallCategoryService;
	@Autowired
	private MallBannerService mallBannerService;
	@Autowired
	private MallSearchBizImpl mallSearchBizImpl;
	@Autowired
	private HomeBizImpl homeBizImpl;
	@Autowired
	private MallService mallService;
	@Autowired
	ZhiHuiMallBiz zhiHuiMallBiz;
	@Autowired
	private MallCommentBizImpl mallCommentBizImpl;
	@Autowired
	private MallGroupbBuyService mallGroupBuyService;
	@Autowired
	private MallFeatureService mallFeatureService;
	@Autowired
	private ProvinceService provinceService;
	@Autowired
	private CityService cityService;
	@Autowired
	private AreaService areaService;
	@Autowired
	private MallActivityService mallActivityService;
	@Autowired
	private MallActivityBookService mallActivityBookService;

	
	/**
	 * 列表页
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("mallList")
	@UserLoginMessage
	public String mallList(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		Integer onlyKey = HttpUtil.getCommkeyId(request, response);
		//美食页banner图
		MallBannerQuery foodBannerQuery = new MallBannerQuery();
		Page bannerPage = Page.getInitPage(5);
		foodBannerQuery.setPage(bannerPage);
		foodBannerQuery.setOnlyKey(onlyKey);
		foodBannerQuery.setType(BannerEnum.MEISHIBANNER.getCode());
		foodBannerQuery.setStatus(BannerEnum.DOBANNER.getCode());
		List<MallBanner> foodBannerList = this.mallBannerService.getMallBannerByMallQuery(foodBannerQuery);
		modelMap.put("foodBannerList", foodBannerList);
		mallListModelMap(request,response,modelMap);
		return "/home/list/mallList";

	}
	
	
	public ModelMap mallListModelMap(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap){
		Integer onlyKey = HttpUtil.getCommkeyId(request, response);
		
		// 设置分页
		Page page = new Page();
		int pageNo = HttpUtil.getIntegerValueHasDefaultFromRequest(request, "pageNo", 1);
		int pageSize = HttpUtil.getIntegerValueHasDefaultFromRequest(request, "pageSize", PageUtil.PAGE_SIZE_APP);
		page.setPageByPageNoAndPageSize(pageNo, pageSize);
		
		// 接收参数:一级类目，二级类目，onlyKey
		Integer categoryId = HttpUtil.getIntegerValueFromRequest(request, "categoryId");// 传递的一级类目
		MallCategory mallCategory = mallCategoryService.getMallCategoryById(categoryId);//一级类目对象
		Integer secCategoryId = HttpUtil.getIntegerValueFromRequest(request, "secCategoryId");// 传递的二级类目
		MallCategory secMallCategory = null;//二级类目对象
		if (secCategoryId != null && secCategoryId > 0) {
			secMallCategory = mallCategoryService.getMallCategoryById(secCategoryId);
		}
		
		//二级类目的对象集合
		List<MallCategory> secMallCategoryList = this.mallCategoryService.getSecMallCategoryListByParentMallCategoryId(categoryId);
		
		//排序方式
		String orderBy = request.getParameter("orderBy");// gov_ping_fen  user_ping_fen    mall_per_capita
		Integer searchPrice = HttpUtil.getIntegerValueHasDefaultFromRequest(request, "searchPrice", 0);// 0:无   1：50以下   2：50-100   3：100以上
		Integer searchPingtaiPingFen = HttpUtil.getIntegerValueHasDefaultFromRequest(request, "searchPingtaiPingFen", 0);// 0:无 1:A级2:B级3:C级
		Long ssig = HttpUtil.getLongValueFromRequest(request,"ssig");
		
		//关键字
		String keyWord = request.getParameter("keyWord"); 
		if(StringUtil.isNotBlank(keyWord)){
			try {
				keyWord = URLDecoder.decode(URLDecoder.decode(keyWord, "UTF-8"), "UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		
		//起止价格
		//Long beginPrice = HttpUtil.getLongValueFromRequest(request, "beginPrice");
		//Long endPrice = HttpUtil.getLongValueFromRequest(request, "endPrice");
		
		//查询条件
		MallSearchQuery mallSearchQuery = new MallSearchQuery();
		mallSearchQuery.setCommKeyId(onlyKey);
		mallSearchQuery.setKeyWord(keyWord);
		mallSearchQuery.setMallMainCategoryId(categoryId);
		mallSearchQuery.setMallSecCategoryId(secCategoryId);
		mallSearchQuery.setOrderBy(orderBy);
		mallSearchQuery.setSearchPingtaiPingFen(searchPingtaiPingFen);
		mallSearchQuery.setSsig(ssig);
		mallSearchQuery.setSearchPrice(searchPrice);
		//mallSearchQuery.setBeginPrice(beginPrice);
		//mallSearchQuery.setEndPrice(endPrice);
		List<MallListVO> mallVOList =this.mallSearchBizImpl.getMallVOList(mallSearchQuery, page);
		
		//分页后得到的页数
		int pages = 0;
		if(mallVOList != null && mallVOList.size() > 0){
			pages = this.homeBizImpl.getPages(mallVOList.size(), pageSize);
		}
		
		modelMap.put("onlyKey", onlyKey);
		modelMap.put("secMallCategoryList", secMallCategoryList);
		modelMap.put("mallCategory", mallCategory);
		modelMap.put("secMallCategory", secMallCategory);
		modelMap.put("onlyKey", onlyKey);
		modelMap.put("mallVOList", mallVOList);
 		modelMap.put("pageNo", pageNo);
		modelMap.put("pages", pages);
		return modelMap;
	}
	
	/**
	 * 美食详情页
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("mallDetail")
	@UserLoginMessage
	public String mallDetail(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap){
		int app = APPCookieUtil.urlFromApp(request);
		modelMap.put("app", app);
		User user = WebUtil.getCurrentUser(request);
		modelMap.put("user", user);
		
		Long mallId = HttpUtil.getLongValueFromRequest(request, "mallId");
		Mall mall = this.mallService.getMallById(mallId);
		if(mall == null){
			return null;
		}
		
		//各详情页的跳转
		if(mall.getMallCategoryId() == 7){//景点详情页的跳转
			return "redirect:/zhoubianyou/jingdianDetail.htm?mallId="+mall.getId();
		}else if(mall.getMallCategoryId() == 2 && mall.getMallSecCategoryId() == 42){//民宿详情页的跳转
			return "redirect:/zhoubianyou/minsuDetail.htm?mallId="+mall.getId();
		}
		
		mallDetailModelMap(request,response,modelMap);
		return "home/list/mallDetail";
	}
	
	
	
	
	public ModelMap mallDetailModelMap(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap){
		Long mallId = HttpUtil.getLongValueFromRequest(request, "mallId");
		Mall mall = this.mallService.getMallById(mallId);
		User user = WebUtil.getCurrentUser(request);
		Integer onlyKey = HttpUtil.getCommkeyId(request, response);
		
		//二维火店铺
		if(mall.getSsig()==1){
			if (user != null) {
				MallItemOrder fireOrder = null;
				List<MallItemVO> userZhiHuiTradeDetailVOList = null;
				fireOrder = zhiHuiMallBiz.getTodayMallFireOrderByUserAndTime(mallId, user.getId());
				if (fireOrder != null) {
					String itemListJson = fireOrder.getBuyItemsJson();
					if (itemListJson != null) {
						userZhiHuiTradeDetailVOList = JSONArray.parseArray(itemListJson, MallItemVO.class);
					}
					modelMap.put("userZhiHuiTradeDetailVOList", userZhiHuiTradeDetailVOList);
					modelMap.put("fireOrder", fireOrder);
					modelMap.put("tipStatus", 1);//为1的时候用户已有订单，弹窗给用户提示
				}

			}
		}
		
		//二级类目
		MallCategory secMallCategory = this.homeBizImpl.getMallCategoryByCategoryId(mall.getMallSecCategoryId());
		//团购列表
		List<MallGroupBuy> mallGroupBuyList = this.mallGroupBuyService.getMallGroupBuyListByMallId(mallId, 0);
		//明星店铺
		List<MallFeature> starFeatureList = this.mallFeatureService.getMallFeatureByMallIdAndType(mallId, MallFeatureEnum.STARMALL.getType());
		//买单店铺
		MallFeature maidanFeature = null;
		if(this.mallFeatureService.getMallFeatureByMallIdAndType(mallId, MallFeatureEnum.MAIDAN.getType()) != null && 
				this.mallFeatureService.getMallFeatureByMallIdAndType(mallId, MallFeatureEnum.MAIDAN.getType()).size() > 0){
			maidanFeature = this.mallFeatureService.getMallFeatureByMallIdAndType(mallId, MallFeatureEnum.MAIDAN.getType()).get(0);
		}
		
		//店铺公告
		List<MallFeature> noticeFeatureList = this.mallFeatureService.getMallFeatureByMallIdAndType(mallId, MallFeatureEnum.MALLNOTICE.getType());
		
		MallActivityQuery mallActivityQuery = new MallActivityQuery();
		mallActivityQuery.setMallId(mallId);
		mallActivityQuery.setStatus(MallActivityEnum.UNUSINGACTIVIVTY.getCode());
		mallActivityQuery.setCommkey(onlyKey);
		//抽奖活动
		mallActivityQuery.setType(MallActivityEnum.CHOUJIANGACTIVITY.getCode());
		MallActivity choujiangActivity = null;
		if(this.mallActivityService.getMallActivityList(mallActivityQuery, null) != null &&
				this.mallActivityService.getMallActivityList(mallActivityQuery, null).size() > 0){
			choujiangActivity = this.mallActivityService.getMallActivityList(mallActivityQuery, null).get(0);
		}
		
		//商家发起的活动
		mallActivityQuery.setType(MallActivityEnum.SELLERACTIVITY.getCode());
		List<MallActivity> sellerActivityList = this.mallActivityService.getMallActivityList(mallActivityQuery, null);
		//用户发起的活动
		mallActivityQuery.setType(MallActivityEnum.USERACTIVITY.getCode());
		List<MallActivity> userActivityList = this.mallActivityService.getMallActivityList(mallActivityQuery, null);
		//参加该店的活动人员
		List<MallActivityBook> mallActivityBookList = null;
		if(sellerActivityList != null && sellerActivityList.size() > 0){
			for(MallActivity sellerActivity : sellerActivityList){
				if( (this.mallActivityBookService.getMallActivityBookListByMallActivityId(sellerActivity.getId())) != null  ){
					mallActivityBookList = this.mallActivityBookService.getMallActivityBookListByMallActivityId(sellerActivity.getId());
				}
			}
		}
		
		//评论
		List<MallCommentVO> mallCommentVOList = this.mallCommentBizImpl.getMallCommentVOListByMallId(mallId, 0, 0,
				Page.getInitPage(PageUtil.PAGE_SIZE_COMMENT));
		int pages = 0;
		if(mallCommentVOList != null && mallCommentVOList.size() > 0){
			pages = this.homeBizImpl.getPages(mallCommentVOList.size(), PageUtil.PAGE_SIZE_COMMENT);
		}
		
		 //卖家阿里百川uid
		AliTonghua aliTonghua = this.homeBizImpl.getSellerALiUserInfo(mall);
		modelMap.put("aliTonghua", aliTonghua);
		
		modelMap.put("mall", mall);
		modelMap.put("user", user);
		modelMap.put("onlyKey", onlyKey);
		modelMap.put("maidanFeature", maidanFeature);
		modelMap.put("choujiangActivity", choujiangActivity);
		modelMap.put("secMallCategory", secMallCategory);
		modelMap.put("mallGroupBuyList", mallGroupBuyList);
		modelMap.put("starFeatureList", starFeatureList);
		modelMap.put("noticeFeatureList", noticeFeatureList);
		modelMap.put("sellerActivityList", sellerActivityList);
		modelMap.put("userActivityList", userActivityList);
		modelMap.put("mallActivityBookList", mallActivityBookList);
		modelMap.put("mallCommentVOList", mallCommentVOList);
		modelMap.put("pages", pages);
		return modelMap;
	}

}
